package com.fashiontextile.designer.service.impl;

import com.fashiontextile.common.entity.R;
import com.fashiontextile.common.utils.IdUtils;
import com.fashiontextile.designer.entity.Designer;
import com.fashiontextile.designer.entity.DesignerWorks;
import com.fashiontextile.designer.entity.WorksAlbum;
import com.fashiontextile.designer.mapper.DesignerMapper;
import com.fashiontextile.designer.service.DesignerService;
import com.fashiontextile.user.entity.Comment;
import com.fashiontextile.user.entity.Image;
import com.fashiontextile.user.feign.UserClient;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class DesignServiceImpl implements DesignerService {
    private DesignerMapper designerMapper;
    private IdUtils idUtils;
    private UserClient userClient;

    @Override
    public Integer insertDesigner(Designer designer) {
        return designerMapper.insertDesigner(designer);
    }

    @Override
    public Integer updateDesignerInfo(Designer newdesignerInfo, long uid) {
        newdesignerInfo.setModified(new Date());
        return designerMapper.updateDesignerInfo(newdesignerInfo, uid);
    }

    @Override
    public Long getFdidByUid(long uid) {
        return designerMapper.getFdidByUid(uid);
    }

    @Override
    public Integer addAlbum(WorksAlbum worksAlbum) {
        return designerMapper.addAlbum(worksAlbum);
    }

    @Override
    public Integer updateAlbum(WorksAlbum worksAlbum) {
        return designerMapper.updateAlbum(worksAlbum);
    }

    @Override
    public boolean isExistAlbum(long waid) {
        return designerMapper.isExistAlbum(waid);
    }

    @Override
    public Integer updataAlbumState(long waid) {
        return designerMapper.updataAlbumState(waid);
    }

    @Override
    public R<?> addDesignerWork(DesignerWorks designerWorks) {
        // 设置id
        long dwid = idUtils.nextId();
        designerWorks.setDwid(dwid);

        // 设置图片id
        for (Image image : designerWorks.getImgs()) {
            image.setPointTo(dwid);
        }

        // 上传图片链接
        if (!userClient.uploadImage(designerWorks.getImgs()).isSuccess()) {
            throw new RuntimeException("上传图片链接失败");
        }

        if (designerMapper.addDesignerWork(designerWorks) > 0) {
            return R.success(dwid);
        }
        return R.fail("发布作品失败");
    }

    @Override
    public boolean isExistWorks(long dwid) {
        return designerMapper.isExistWorks(dwid);
    }

    @Override
    public boolean updateWorksByDwid(DesignerWorks designerWorks, long dwid) {
        return designerMapper.updateWorksByDwid(designerWorks, dwid) > 0;
    }

    @Override
    public Integer updataworksState(long dwid) {
        return designerMapper.updataWorksState(dwid);
    }

    @Override
    public Designer selectDesignerByid(long fdid) {
        return designerMapper.selectDesignerByid(fdid);
    }

    @Override
    public List<WorksAlbum> getAllAlbumByFdid(long fdid) {
        return designerMapper.getAllAlbumByFdid(fdid);
    }

    @Override
    public WorksAlbum getAlbumAndWorks(long waid) {
        WorksAlbum worksAlbum = designerMapper.getAlbumAndWorks(waid);
        if (worksAlbum != null) {
            SetImgeIntoWorks(worksAlbum.getWorksList());
        }
        return worksAlbum;
    }

    @Override
    public DesignerWorks getWorksByDwid(long wdid) {
        DesignerWorks designerWorks = designerMapper.getWorksByDwid(wdid);
        if (designerWorks != null) {
            designerWorks.setImgs(designerMapper.selectImage(30L, designerWorks.getDwid()));
        }
        return designerWorks;
    }

    @Override
    public Designer getDesignerByUid(long uid) {
        return designerMapper.getDesignerByUid(uid);
    }

    @Override
    public List<Designer> getDesigner(Integer num, String type) {
        if (type.equals("random")) {
            return designerMapper.getRandDesigner(num);
        }
        if (type.equals("new")) {
            return designerMapper.getNewestDesigner(num);
        }
        return null;
    }

    @Override
    public R<?> getWorksS(String sort, Integer num) {
        PageHelper.startPage(num, 10);
        if ("new".equals(sort)) {
            List<DesignerWorks> worksList = designerMapper.getNewWorksS();
            SetImgeIntoWorks(worksList);
            PageInfo<DesignerWorks> workSs = new PageInfo<>(worksList);
            return R.success(workSs);
        }
        if ("random".equals(sort)) {
            List<DesignerWorks> randomWorksS = designerMapper.getRandomWorksS();
            SetImgeIntoWorks(randomWorksS);
            PageInfo<DesignerWorks> designerWorksPageInfo = new PageInfo<>(randomWorksS);
            return R.success(designerWorksPageInfo);
        }
        return R.fail("查询不到作品集");
    }

    @Override
    public R<?> CommentWorks(Long dwid, Comment comment, Long uid) {
        comment.setCid(idUtils.nextId());
        comment.setCtid(dwid);
        comment.setUid(uid);
        Integer commentResult = designerMapper.CommentWorks(comment);
        return commentResult > 0 ? R.success("评论成功") : R.fail("系统出错，评论失败！");
    }

    @Override
    public R<?> selectCommentByctid(Long aid) {
        List<Comment> parentComments = designerMapper.selectAllParantComment(aid);
        List<Long> parentCid=new ArrayList<>();
        for (Comment temp:parentComments) {
            parentCid.add(temp.getCid());
        }
        List<Comment> allChildreComment=designerMapper.selectAllChildrenComment(aid);
        List<Comment> result=handParentChildreComment(parentComments,allChildreComment);
        return R.success(result);
    }

    @Override
    public List<Comment> handParentChildreComment(List<Comment> parentComments,List<Comment> allChildreComment) {
        for(Comment parentCommen:parentComments){
            List<Comment> temp=new ArrayList<>();
            for (Comment childreComment:allChildreComment){
                if(childreComment.getParent().equals(parentCommen.getCid())){
                    temp.add(childreComment);
                }
            }
            parentCommen.setChildren(temp);
            handParentChildreComment(temp,allChildreComment);
        }
        return parentComments;
    }

    @Override
    public R<?> addPopularity(Long dwid) {
        long oldNum = designerMapper.selectOldPopularity(dwid);
        long newNum=++oldNum;
        Integer updateResult = designerMapper.updatePopularity(dwid,newNum);
        return updateResult > 0 ? R.success("点赞成功！") : R.fail("系统故障，点赞失败");
    }

    public void SetImgeIntoWorks(List<DesignerWorks> designerWorks) {
        List<Long> arr = new ArrayList<>();
        // 存储作品id
        for (DesignerWorks dw : designerWorks) {
            arr.add(dw.getDwid());
        }
        // 获取作品图片
        List<Image> imgs = designerMapper.selectImages(30L, arr);
        // 将作品图片添加到作品中
        for (DesignerWorks dw : designerWorks) {
            List<Image> temp = new ArrayList<>();
            for (Image img : imgs) {
                if (img.getPointTo().equals(dw.getDwid())) {
                    temp.add(img);
                }
            }
            dw.setImgs(temp);
        }
    }
}
